package com.rc.evcharger.dto.terminal;


import com.rc.evcharger.common.annotation.ByteLength;
import com.rc.evcharger.common.terminal.Result;
import com.rc.evcharger.common.utils.StringUtil;

import java.lang.reflect.Field;

/**
 * 车辆vin码
 */
public class VinVehicleResult implements Result {

    private Long pileId;

    @ByteLength(length = 1)
    private String gunNum;

    @ByteLength(length = 17)
    private String vin;

//    @ByteLength(length = 4)
//    private String obligate; // 预留字段;

    public VinVehicleResult() {
    }

    public VinVehicleResult(String str) {
        try {
            Field[] fields = this.getClass().getDeclaredFields();
            for (int i = 0; i < fields.length; i++) {
                ByteLength bl = fields[i].getAnnotation(ByteLength.class);
                if (bl == null) {
                    continue;
                }
                int len = bl.length() * 3 - 1;
                String value = str.substring(0, len);
                if (str.length() > len)
                    str = str.substring(len + 1);
                setValue(fields[i], bl, value);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    private void setValue(Field field, ByteLength bl, String value) {
        try {
            if (bl.length() == 17) {
                field.set(this, StringUtil.resolveVin(value));
            } else if (bl.length() == 1) {
                field.set(this, value.substring(1, 2));
            } else {
                field.set(this, value.trim());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    public String getGunNum() {
        return gunNum;
    }

    public void setGunNum(String gunNum) {
        this.gunNum = gunNum;
    }

    public String getVin() {
        return vin;
    }

    public void setVin(String vin) {
        this.vin = vin;
    }

//    public String getObligate() {
//        return obligate;
//    }

//    public void setObligate(String obligate) {
//        this.obligate = obligate;
//    }

    public Long getPileId() {
        return pileId;
    }

    public void setPileId(Long pileId) {
        this.pileId = pileId;
    }

    @Override
    public VinVehicleResult getResult(String dataStr) {
        VinVehicleResult vvr = new VinVehicleResult(dataStr);
        return vvr;
    }

    @Override
    public String toString() {
        return "车辆枪号:" + this.getGunNum() + " vin码:" + this.getVin() + " ";
    }

    public static void main(String[] args) {
        VinVehicleResult result = new VinVehicleResult();
        String aa = "01 00 00 01 08 05 02 09 04 04 07 07 03 08 00 00 00 01 00 00 00 00";
        result = result.getResult(aa);
        System.out.println(result.toString());
    }
}
